﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZX.Web.UI.Controls;
using ZX.DBModule;
using ZX.BLL;
using ZX.EHR.Library;
using ZX.DAL.DBExtend;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace  ZX.TR.Web
{
    public partial class trAjax : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string flag = this.GetStringFromQueryString("flag", "");
            switch (flag.ToLower())
            {
                case "issubjectinfoused":
                    IsSubjectInfoUsed();
                    break;
                case "deletesubjectinfo":
                    DeleteSubjectInfo();
                    break;
                case "isquestionnaireused":
                    IsQuestionnaireUsed();
                    break;
                case "deletequestionnaire":
                    Deletequestionnaire();
                    break;
                case "questionnairecopy":
                    QuestionnaireCopy();
                    break;                default:
                    break;
            }

        }

        #region 题目
        /// <summary>
        /// 题目是否被引用了
        /// </summary>
        public void IsSubjectInfoUsed()
        {
            try
            {
                string id = this.GetStringFromQueryString("ID", "");
                if (id == string.Empty)
                {
                    HttpContext.Current.Response.Write("ID为空"); //ID为空
                    return;
                }
                BaseEntity instance = null;
                ZX.BLL.Extend.BaseBussnies baseBll = null;
                string condition = "";
                bool isDelete = true;
                //定义引用表
                string[] tableName = { "TrQuestionnaireAnswer", "TrQuestionnaireSubject" };
                //先查询是否被引用了，如果有引用则不能删除，给提示
                foreach (string s in tableName)
                {
                    instance = ZX.Common.Globals.getEntity(s) as BaseEntity;
                    baseBll = new ZX.BLL.Extend.BaseBussnies(instance);
                    condition = string.Format("TrSubjectInfoID ='{0}'", id);
                    isDelete = (baseBll.GetEntityByCondition(condition) == null);
                    if (!isDelete) break;
                }
                if (isDelete)
                {
                    HttpContext.Current.Response.Write("0");  //未引用
                }
                else
                {
                    HttpContext.Current.Response.Write("有引用则不能修改");  //有引用
                    return;
                }
            }
            catch (Exception e)
            {
                HttpContext.Current.Response.Write("e" + e.Message);
            }
        }

        /// <summary>
        /// 删除题目
        /// </summary>
        public void DeleteSubjectInfo()
        {
            try
            {
                string id = this.GetStringFromQueryString("json", "").Replace("[@@]", "\\");

                if (id == string.Empty)
                {
                    HttpContext.Current.Response.Write("ID为空"); //ID为空
                    return;
                }
                id = "'" + id.Replace(",", "','") + "'";
                BaseEntity instance = null;
                ZX.BLL.Extend.BaseBussnies baseBll = null;
                string condition = "";
                bool isDelete = true;
                //定义引用表
                string[] tableName = { "TrQuestionnaireAnswer", "TrQuestionnaireSubject" };
                //先查询是否被引用了，如果有引用则不能删除，给提示
                foreach (string s in tableName)
                {
                    instance = ZX.Common.Globals.getEntity(s) as BaseEntity;
                    baseBll = new ZX.BLL.Extend.BaseBussnies(instance);
                    condition = string.Format("TrSubjectInfoID in ({0})", id);
                    isDelete = (baseBll.GetEntityByCondition(condition) == null);
                    if (!isDelete) break;
                }
                if (isDelete)
                {
                    //删除子表
                    instance = ZX.Common.Globals.getEntity("TrSubjectItem") as BaseEntity;
                    condition = string.Format("TrSubjectInfoID  in ({0})", id);
                    baseBll = new ZX.BLL.Extend.BaseBussnies(instance);
                    baseBll.delete(condition);

                    //删除主表
                    instance = ZX.Common.Globals.getEntity("TrSubjectInfo") as BaseEntity;
                    condition = string.Format("TrSubjectInfoID  in ({0})", id);
                    baseBll = new ZX.BLL.Extend.BaseBussnies(instance);
                    baseBll.delete(condition);
                    HttpContext.Current.Response.Write("1");
                }
                else
                {
                    HttpContext.Current.Response.Write("有引用则不能删除");  //有引用则不能删除
                    return;
                }
            }
            catch (Exception e)
            {
                HttpContext.Current.Response.Write("e" + e.Message);
            }
        }

        #endregion

        #region 问卷
        /// <summary>
        /// 问卷是否开始调研了
        /// </summary>
        public void IsQuestionnaireUsed()
        {
            try
            {
                string id = this.GetStringFromQueryString("ID", "");
                if (id == string.Empty)
                {
                    HttpContext.Current.Response.Write("ID为空"); //ID为空
                    return;
                }
                BaseEntity instance = null;
                ZX.BLL.Extend.BaseBussnies baseBll = null;
                string condition = "";
                bool isDelete = true;
                //定义引用表
                string[] tableName = { "TrQuestionnaireAnswer"};
                //先查询是否被引用了，如果有引用则不能删除，给提示
                foreach (string s in tableName)
                {
                    instance = ZX.Common.Globals.getEntity(s) as BaseEntity;
                    baseBll = new ZX.BLL.Extend.BaseBussnies(instance);
                    condition = string.Format("TrQuestionnaireID ='{0}'", id);
                    isDelete = (baseBll.GetEntityByCondition(condition) == null);
                    if (!isDelete) break;
                }
                if (isDelete)
                {
                    HttpContext.Current.Response.Write("0");  //未引用
                }
                else
                {
                    HttpContext.Current.Response.Write("有引用则不能修改");  //有引用
                    return;
                }
            }
            catch (Exception e)
            {
                HttpContext.Current.Response.Write("e" + e.Message);
            }
        }

        /// <summary>
        /// 删除问卷
        /// </summary>
        public void Deletequestionnaire()
        {
            try
            {
                string id = this.GetStringFromQueryString("json", "").Replace("[@@]", "\\");

                if (id == string.Empty)
                {
                    HttpContext.Current.Response.Write("ID为空"); //ID为空
                    return;
                }
                id = "'" + id.Replace(",", "','") + "'";
                BaseEntity instance = null;
                ZX.BLL.Extend.BaseBussnies baseBll = null;
                string condition = "";
                bool isDelete = true;
                //定义引用表
                string[] tableName = {"TrQuestionnaireAnswer" };
                //先查询是否被引用了，如果有引用则不能删除，给提示
                foreach (string s in tableName)
                {
                    instance = ZX.Common.Globals.getEntity(s) as BaseEntity;
                    baseBll = new ZX.BLL.Extend.BaseBussnies(instance);
                    condition = string.Format("TrQuestionnaireID in ({0})", id);
                    isDelete = (baseBll.GetEntityByCondition(condition) == null);
                    if (!isDelete) break;
                }
                if (isDelete)
                {
                    //删除子表
                    instance = ZX.Common.Globals.getEntity("TrQuestionnaireSubject") as BaseEntity;
                    condition = string.Format("TrQuestionnaireID  in ({0})", id);
                    baseBll = new ZX.BLL.Extend.BaseBussnies(instance);
                    baseBll.delete(condition);

                    //删除主表
                    instance = ZX.Common.Globals.getEntity("TrQuestionnaire") as BaseEntity;
                    condition = string.Format("TrQuestionnaireID  in ({0})", id);
                    baseBll = new ZX.BLL.Extend.BaseBussnies(instance);
                    baseBll.delete(condition);
                    HttpContext.Current.Response.Write("1");
                }
                else
                {
                    HttpContext.Current.Response.Write("有引用则不能删除");  //有引用则不能删除
                    return;
                }
            }
            catch (Exception e)
            {
                HttpContext.Current.Response.Write("e" + e.Message);
            }
        }

        /// <summary>
        /// 问卷复制
        /// </summary>
        public void QuestionnaireCopy()
        {
            try
            {
                string id = this.GetStringFromQueryString("QuestionnaireID", "");
                string questionnaireName = this.GetStringFromQueryString("QuestionnaireName", "");

                if (id == string.Empty)
                {
                    HttpContext.Current.Response.Write("ID为空"); //ID为空
                    return;
                }
                DbSql dbsql = new DbSql();
                string sqlM = @"insert into TrQuestionnaire([TrQuestionnaireID],[QStyleID],[DeptID],[txtDeptID],[QuestionnaireName],[QAim],
                    [SubjectSum],[PointSum],[QStartTime],[QEndTime],[QTimeLength],[QNotice],[QSource],[IsJumpOver],[QSumup],[IsUsed],
                    [Explain],[AddUser],[AddTime]) 
                    values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}','{16}','{17}','{18}')";
                string sqlD = @"insert into TrQuestionnaireSubject([TrQuestionnaireSubjectID],[TrQuestionnaireID],[TrSubjectInfoID],[Sequence],
                    [SubjectPoint],[IsJumpOver],[AddUser],[AddTime]) 
                    values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}')";

                //取出源问卷
                DataTable TrQuestionnaire = dbsql.ExecuteQuery(string.Format("select * from TrQuestionnaire where TrQuestionnaireID='{0}'",id));
                DataTable TrQuestionnaireSubject = dbsql.ExecuteQuery(string.Format("select * from TrQuestionnaireSubject where TrQuestionnaireID='{0}'", id));
                //开始复制
                //先写入主表
                foreach (DataRow dr in TrQuestionnaire.Rows)
                {
                    string TrQuestionnaireID = Guid.NewGuid().ToString();
                    dbsql.ExecuteSql(string.Format(sqlM, TrQuestionnaireID, dr["QStyleID"].ToString(), dr["DeptID"].ToString(), dr["txtDeptID"].ToString(), questionnaireName, dr["QAim"].ToString(), dr["SubjectSum"].ToString() == "" ? "0" : dr["SubjectSum"].ToString(), dr["PointSum"].ToString() == "" ? "0" : dr["PointSum"].ToString(), dr["QStartTime"].ToString(), dr["QEndTime"].ToString(), dr["QTimeLength"].ToString() == "" ? "0" : dr["QTimeLength"].ToString(), dr["QNotice"].ToString(), dr["QSource"].ToString(), dr["IsJumpOver"].ToString(), dr["QSumup"].ToString(), dr["IsUsed"].ToString(), dr["Explain"].ToString(), this.UserId.ToString(), DateTime.Now));                   
                    //写入子表
                    foreach (DataRow item in TrQuestionnaireSubject.Rows)
                    {
                        dbsql.ExecuteSql(string.Format(sqlD, Guid.NewGuid().ToString(), TrQuestionnaireID, item["TrSubjectInfoID"].ToString(), item["Sequence"].ToString() == "" ? "0" : item["Sequence"].ToString(), item["SubjectPoint"].ToString() == "" ? "0" : item["SubjectPoint"].ToString(), item["IsJumpOver"].ToString(), this.UserId.ToString(), DateTime.Now));
                    }
                }
                HttpContext.Current.Response.Write("1");
            }
            catch (Exception e)
            {
                HttpContext.Current.Response.Write("e" + e.Message);
            }

        }

        #endregion

    }
}